package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.StudyVideo;
import com.dingreading.cloud.admin.entity.StudyVideoExpire;
import com.dingreading.cloud.admin.service.StudyVideoExpireService;
import com.dingreading.cloud.admin.service.StudyVideoLogService;
import com.dingreading.cloud.admin.service.StudyVideoService;
import com.dingreading.cloud.admin.util.CdnUrlUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/system/studyVideo")
@Api(tags = "总部-习作视频")
public class StudyVideoController {

    @Value("${serverUrl}")
    private String serverUrl;
    @Resource
    private CdnUrlUtil cdnUrlUtil;

    @Resource
    private StudyVideoService studyVideoService;
    @Resource
    private StudyVideoExpireService studyVideoExpireService;
    @Resource
    private StudyVideoLogService studyVideoLogService;


    @ApiOperation(value = "视频分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil
    ) {
        Page<StudyVideo> page = studyVideoService.pageList(pageUtil);

        PageDto<StudyVideo> data = new PageDto<>();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "根据视频uid获取视频")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid
    ) {
        StudyVideo r = studyVideoService.getByUid(uid);
        return R.ok(r);
    }


    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/修改视频")
    @PostMapping(value = "/add", produces = "application/json; charset=UTF-8")
    public R<Object> add(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "视频名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "视频地址") @RequestParam(value = "url", required = false) String url
    ) throws Exception {
        StudyVideo data = null;
        if (StringUtils.isBlank(uid)) {
            data = new StudyVideo();
            data.setUid(KitUtil.getUuid());
            data.setUrl(url);
            data.setEnabled(1);
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
        } else {
            data = studyVideoService.getByUid(uid);
            if (data == null)
                return R.failGet();
        }
        data.setName(name);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        studyVideoService.saveOrUpdate(data);
        return R.ok();
    }


    @ApiOperation(value = "更新视频的启用状态")
    @PostMapping(value = "/updateVideoEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateVideoEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = studyVideoService.updateEnabled(uid, enabled, headStaffUid, headStaffName);
        if (!l)
            return R.fail("更新失败");
        return R.ok();
    }


    @ApiOperation(value = "删除视频")
    @PostMapping(value = "/deleteVideo", produces = "application/json; charset=UTF-8")
    public R<Object> deleteVideo(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid
    ) {
        StudyVideo video = studyVideoService.getByUid(uid);
        if (video == null)
            return R.fail("未查询到视频");

        boolean b = studyVideoExpireService.existsByVideoUid(video.getUid());
        if (b)
            return R.fail("视频下已添加过期视频记录，不能删除");

        int i = studyVideoService.deleteByUid(uid);
        if (i <= 0)
            return R.failDel();
        return R.ok();
    }


    @ApiOperation(value = "获取视频的cdn访问链接")
    @PostMapping(value = "/getCndUrl", produces = "application/json; charset=UTF-8")
    public R<Object> getCndUrl(
            HttpServletRequest request,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid
    ) {

        StudyVideo video = studyVideoService.getByUid(uid);
        if (video == null)
            return R.fail("未查询到视频");

        // 文件获取cdn访问链接
        R<String> r = cdnUrlUtil.getCdnUrl(request, video.getUrl());
        if (R.isError(r))
            return R.fail(r.getMsg());

        return R.ok(r.getData());
    }


    @ApiOperation(value = "过期视频-创建一个有过期时间的视频访问链接")
    @PostMapping(value = "/addExpireVideo", produces = "application/json; charset=UTF-8")
    public R<Object> addExpireVideo(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "过期时间") @RequestParam(value = "expireTime", required = false) String expireTime
    ) throws Exception {
        StudyVideo video = studyVideoService.getByUid(uid);
        if (video == null)
            return R.fail("未查询到视频");
        if (!video.getEnabled().equals(1))
            return R.fail("视频已被禁用");

        StudyVideoExpire data = new StudyVideoExpire();
        data.setUid(KitUtil.getUuid());
        data.setVideoUid(uid);
        data.setExpireTime(DateUtil.parseDateFromStr(expireTime));
        data.setEnabled(1);
        data.setAddStaffUid(headStaffUid);
        data.setAddStaffName(headStaffName);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        studyVideoExpireService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "过期视频-更新过期视频的启用状态")
    @PostMapping(value = "/updateVideoExpireEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateVideoExpireEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "过期视频uid") @RequestParam(value = "videoExpireUid") String videoExpireUid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = studyVideoExpireService.updateEnabled(videoExpireUid, enabled, headStaffUid, headStaffName);
        if (!l)
            return R.fail("更新失败");
        return R.ok();
    }

    @ApiOperation(value = "过期视频-删除过期视频")
    @PostMapping(value = "/deleteVideoExpire", produces = "application/json; charset=UTF-8")
    public R<Object> deleteVideoExpire(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "过期视频uid") @RequestParam(value = "videoExpireUid") String videoExpireUid
    ) {
        StudyVideoExpire videoExpire = studyVideoExpireService.getByUid(videoExpireUid);
        if (videoExpire == null)
            return R.failGet();

        int i = studyVideoExpireService.deleteByUid(videoExpireUid);
        if (i <= 0)
            return R.failDel();
        return R.ok();
    }

    @ApiOperation(value = "过期视频-获取视频的过期视频列表")
    @PostMapping(value = "/getVideoExpireList", produces = "application/json; charset=UTF-8")
    public R<Object> getVideoExpireList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "视频uid") @RequestParam(value = "uid") String uid
    ) {
        List<StudyVideoExpire> list = studyVideoExpireService.listByVideoUid(uid);
        return R.ok(list);
    }

    @ApiOperation(value = "过期视频-获取视频的网页地址")
    @PostMapping(value = "/getUrl", produces = "application/json; charset=UTF-8")
    public R<Object> getUrl(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "过期视频uid") @RequestParam(value = "videoExpireUid") String videoExpireUid
    ) {
        StudyVideoExpire videoExpire = studyVideoExpireService.getByUid(videoExpireUid);
        if (videoExpire == null)
            return R.failGet();
        if (!videoExpire.getEnabled().equals(1))
            return R.fail("已禁用");

        StudyVideo video = studyVideoService.getByUid(videoExpire.getVideoUid());
        if (video == null)
            return R.fail("未查询到视频");
        if (!video.getEnabled().equals(1))
            return R.fail("视频已禁用");

        // 习作网址：https://test.dingreading.com/studyVideo/?uid=
        String url = serverUrl + "/studyVideo?uid=" + videoExpireUid;

        return R.ok(url);
    }


}
